//
//  RankView.m
//  demo2
//
//  Created by mysj on 16/7/5.
//  Copyright © 2016年 zeng. All rights reserved.
//

#define WH   57

#import "RankView.h"


@interface RankView () {
    
    int lv;
}

@property (nonatomic, strong) UIImageView *     imgView;
@property (nonatomic, strong) UILabel *         LvLabel;
@property (nonatomic, strong) NSMutableArray *  items;
@end

@implementation RankView

- (void)setBackImgName:(NSString *)imgName andTitleText:(NSString *)titleText andHiddenCount:(NSInteger)hiddenCount {

}

#pragma mark -
- (void)setHiddenCount:(int)hiddenCount {
    _hiddenCount = hiddenCount;
    
    if (hiddenCount >= 8)
    {
        hiddenCount = 8;
        UIImageView *imageView = self.items.lastObject;
        [UIView animateWithDuration:0.8 animations:^{ imageView.alpha = 1; }];
        [self upLv:(lv += 1)];
        
        for (int i = 0; i< hiddenCount; i++) {
            UIImageView *imageView = self.items[i];
            [UIView animateWithDuration:0.8 animations:^{ imageView.alpha = 0; }];
        }
        _hiddenCount = 0;
        return;
    }

    for (int i = 0; i < hiddenCount; i++)
    {
        UIImageView *imageView = self.items[i];
        [UIView animateWithDuration:0.8 animations:^{ imageView.alpha = 1.0f; }];
    }
}

- (void)upLv:(int)ulv {
    self.LvLabel.text = [NSString stringWithFormat:@"%d",ulv];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
   self.hiddenCount +=1;
}


#pragma mark -

+ (instancetype)rank {
    return [[self alloc] init];
}

- (instancetype)init {
    if (self = [super init])
    {
        [self prepareItems];
    }
    return self;
}

- (void)prepareItems {
    
    if (self.LvText)
    {
        lv = [self.LvText intValue];
        [self upLv:lv];
    }
    
    [self addSubview:self.imgView];
    CGPoint loc = CGPointMake(WH *.5, WH *.5);
    self.imgView.center = loc;
    
    [self addSubview:self.LvLabel];
    self.LvLabel.center = loc;
    
    for (int i = 0; i < 8; i++)
    {
       UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"rankBtn_item_%d",i]];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:img];
        imageView.layer.anchorPoint = CGPointMake(0, 0);
        imageView.center = [self getImgViewCenter:i];
        imageView.alpha = .1;
        imageView.tag = i;
        [self.items addObject:imageView];
        [self addSubview:imageView];
    }
    NSLog(@"%lu", (unsigned long)self.items.count);
    //逆向遍历
//    NSEnumerator *enumerator = [self.items reverseObjectEnumerator];
//    self.items = (NSMutableArray*)[enumerator allObjects];
    
}

- (CGPoint)getImgViewCenter:(int)index {
    switch (index)
    {
        case 0:
            return  CGPointMake(28, 10.2);
            break;
        case 1:
            return  CGPointMake(40, 17.2);
            break;
        case 2:
            return  CGPointMake(40.5, 29);
            break;
        case 3:
            return  CGPointMake(28.5, 38.5);
            break;
        case 4:
            return  CGPointMake(15, 39.8);
            break;
        case 5:
            return  CGPointMake(9, 29);
            break;
        case 6:
            return  CGPointMake(8.3, 17);
            break;
        default: return CGPointMake(14.3, 10);
            
            break;
    }
    return CGPointZero;
}

#pragma mark - 

- (NSMutableArray *)items {
    if (_items == nil) {
        _items = [NSMutableArray arrayWithCapacity:9];
    }
    return _items;
}

- (UILabel *)LvLabel {
    if (_LvLabel == nil) {
        _LvLabel = [[UILabel alloc] init];
        _LvLabel.text = @"0";
        _LvLabel.textColor = [UIColor whiteColor];
        [_LvLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];
        [_LvLabel sizeToFit];
    }
    return _LvLabel;
}

- (UIImageView *)imgView {
    if (_imgView == nil) {
        _imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"rankBtn"]];
    }
    return _imgView;
}


//- (void)drawRect:(CGRect)rect {
//
//    CGContextRef context = UIGraphicsGetCurrentContext();
//    //    72 100 129
////    CGContextSetRGBStrokeColor(context,72/255.0,100/255.0,129/255.0,1.0);
//    CGContextSetRGBStrokeColor(context,1,0,1,1.0);
//    CGContextSetLineWidth(context, 4.0);
//    CGContextAddArc(context, rect.size.width * 0.5, rect.size.height * 0.5, 17, 0, 2 * M_PI, 0); //添加一个圆
//    CGContextDrawPath(context, kCGPathStroke); //绘制路径
//    CGContextDrawPath(context, kCGPathFill);//绘制填充
    

//    CGContextSetLineCap(context, kCGLineCapRound);
//    CGContextSetLineWidth(context, 1.0);
//    CGContextSetAllowsAntialiasing(context, true);
//    CGContextSetRGBStrokeColor(context, 70.0 / 255.0, 241.0 / 255.0, 241.0 / 255.0, 1.0);
//    CGContextBeginPath(context);
//    
//    CGContextMoveToPoint(context, WH *.5, 0);  //起点坐标
//    CGContextAddLineToPoint(context, WH *.5, WH);
//    
//    CGContextStrokePath(context);
//}
@end
